const crypto = require('crypto')
const { exec, escape } = require('../db/mysql')
const login = (userName, userPass) => {
  const user = escape(userName)
  const str = `password=${userPass}&key=hua126mail@126.com`
  let pwd = crypto.createHash('md5').update(str).digest('hex')
  pwd = escape(pwd) // 7b32fad8970bdc04d30b6fcbc7c26740
  const sql = `select * from users where username=${user} and pwd=${pwd}`
  return exec(sql).then(data => {
    if (data.length) {
      return data[0]
    } else {
      return null
    }
  })
};

module.exports = {
  login,
};
